@extends('admin.layout.layout', ['row' => @$page])
@section('style')
    <style>
        .table .tth{
            background-color: rgba(0, 0, 0, 0.05);
            width: 150px;
            font-weight: bold;
        }
        .table .ttd{
            width: 350px;
        }
        .table .ttt{
            padding: 0;
        }
        .ttt .table{
            margin: 0;
        }
        .ttt .table img + p{
            text-align: center;
            margin: 10px 0 0;
        }
        .tst{
            color: #00b050;
            font-weight: 700;
            text-align: center;
            font-size: 1.25rem;
        }
    </style>
@endsection
@section('contain')

    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>
        @if($data->approved != 1)
            <x-warranty-message :warranty="$data"></x-warranty-message>
        @endif
        <div>
            <table class="table s3">
                <tbody>
                <tr>
                    <td class="tth">{{ ts('warrantyid') }}</td>
                    <td class="ttd">{{ $data->warranty_id }}</td>
                    @if($data->approved == 1 || $data->approved == 2)
                        <td class="tth">{{ ts('DQZT') }}</td>
                        <td class="ttd">{{ ts('STA'.$data->approved) }}</td>
                        {{--
                        <td class="tth">{{ ts('STATE'.$data->approved) }}</td>
                        <td class="ttd">{{ fdate2($data->approved_date) }}</td>--}}
                        <td class="tth">{{ ts('tgrq') }}</td>
                        <td class="ttd">{{ fdate($data->approved_date) }}</td>
                    @else
                        <td class="ttd" colspan="4"></td>
                    @endif
                    {{--<td class="tth">{{ ts('azfs') }}</td>
                    <td class="ttd" colspan="4">{{ ts('ptaz') }}</td>--}}
                    {{--@empty($data->pre_id)
                        <td class="tth">{{ ts('azfs') }}</td>
                        <td class="ttd" colspan="4">{{ ts('ptaz') }}</td>
                    @else
                        <td class="tth">{{ ts('azfs') }}</td>
                        <td class="ttd">{{ ts('tcaz') }}</td>
                        <td class="tth">{{ ts('precutid') }}</td>
                        <td class="ttd">
                            {{ getPPId($data->pre_id) }}
                        </td>
                    @endif--}}
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('CAROWNERINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('CUSTOMERNAME') }}</td>
                    <td class="ttd">{{ $data->first_name }}</td>
                    <td class="tth">{{ ts('phonenumber') }}</td>
                    <td class="ttd">{{ $data->extension }} {{ substr($data->phone_number, 0, 3)."****".substr($data->phone_number, -4) }}</td>
                    <td class="tth">{{ ts('email') }}</td>
                    <td class="ttd">{{ $data->email_address }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('region') }}</td>
                    <td>{{ ts('region_master.'.$data->region_id) }}</td>
                    <td class="tth">{{ ts('province') }}</td>
                    <td>{{ ts('province_master.'.$data->province_id) }}</td>
                    <td class="tth">{{ ts('city') }}</td>
                    <td>{{ ts('cities.'.$data->city) }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('CZDZ') }}</td>
                    <td colspan="3">{{ $data->address }}</td>
                    {{--<td class="tth">{{ ts('zip') }}</td>
                    <td>{{ $data->zip }}</td>--}}
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLEINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('clnf') }}</td>
                    <td>{{ $data->year_id }}</td>
                    <td class="tth">{{ ts('make') }}</td>
                    <td>{{ ts('make_master.'.$data->make_id) }}</td>
                    <td class="tth">{{ ts('model') }}</td>
                    <td>
                        @if (strtotime($data->approved_date) >= strtotime('2020-12-10'))
                            {{ ts('model_master.'.$data->model_id) ?: $data->model_id }}
                        @else
                            {{ !is_numeric($data->model_id) ? $data->model_id : (ts('model_master.'.$data->model_id) ?: $data->model_id) }}
                        @endif
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('licenseplate') }}</td>
                    <td>{{ $data->license_plate }}</td>
                    <td class="tth">{{ ts('vinnumber') }}</td>
                    <td colspan="3">{{ $data->vin_number }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('sdmc') }}</td>
                    <td>
                        {{ getWDDName($data) }}
                        {{--{{ getDJc($data->user_id) }}--}}
                    </td>
                    <td class="tth">{{ ts('qudao') }}</td>
                    <td colspan="">
                        {{ getWDName($data) }}
                        {{--{{ getJc($data->user_id) }}--}}
                    </td>
                    @if($data->precut_id)
                        <td class="tth">{{ ts('precutid') }}</td>
                        <td class="ttd bg-light">{{ getNPid($data->precut_id) }}</td>
                    @endif
                </tr>
                <tr>
                    <td class="tth">{{ ts('INSTALLERNAME') }}</td>
                    <td>{{ $data->installer_name }}</td>
                    <td class="tth">{{ ts('INSTALLATIONDATE') }}</td>
                    <td>
                        {{ fdate(@$data->installation_date) }}
                    </td>
                    <td class="tth">{{ ts('INSTALLERPRICE') }}</td>
                    <td>{{ $data->installation_price }}</td>
                </tr>
{{--                @if (!$data->precut_id)--}}
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONPICTURE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt p-3">
                        <x-warranty-image :warranty="$data"></x-warranty-image>
                        {{--@include('admin.subview.aztp-tb')--}}
                    </td>
                </tr>
{{--                @endif--}}

                @if (empty($data->precut_id))
                <tr>
                    <td colspan="6" class="ttt">
                        <x-warranty-image-demo></x-warranty-image-demo>
                    </td>
                </tr>
                @endif


                @empty(@$data->precut_id)
                    <tr>
                        <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                    </tr>
                    <tr>
                        <td colspan="6" class="ttt">
                            @include('admin.subview.coverage-tb', ['coverages'=>$data->warranty_to_vehicle_coverage])
                        </td>
                    </tr>
                @else
                    <tr>
                        <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                    </tr>
                    <tr>
                        <td colspan="6" class="ttt">
                            <x-new-precut-fpart :warranty="$data"></x-new-precut-fpart>
                        </td>
                    </tr>
                @endif
                <tr>
                    <td colspan="6" class="tst">{{ ts('BCSM') }}</td>
                </tr>
                <tr>
                    <td colspan="6" height="64">{{ $data->additional_comments }}</td>
                </tr>
                @if($data->approved == 2)
                    <tr>
                        <td colspan="6" class="tst">{{ ts('jjyy') }}</td>
                    </tr>
                    <tr>
                        <td colspan="6" height="64">{{ $data->jjyy() }}</td>
                    </tr>
                @endif
                </tbody>
            </table>

            <div class="mt-4">
                @if($data->approved==1)
                    <button type="button" class="btn btn-warning mr-3" data-toggle="modal" data-target="#editCustomer">{{ ts('XGGKXX') }}</button>
                @elseif($data->approved == 3 || session('admin.user_type')==1)
                    <a class="btn btn-warning" href="/{{ request()->path() }}/edit">{{ ts('xg') }}</a>
                @endif
                @if(session('admin.user_type')==1)

                @else
                    @if($data->approved==3)
                        {{--<a class="btn btn-warning mr-3" href="/{{ request()->path() }}/edit">{{ ts('edit') }}</a>--}}
                        {{--<button class="btn btn-primary mr-3" type="button">{{ ts('XGTCID') }}</button>--}}
                        {{--<span class="btn btn-primary" onclick="handleWFQSH({{ $data->id }})">{{ ts('FQSH') }}</span>--}}
                    @elseif($data->approved==2)
                        <span class="btn btn-danger btn-sm" onclick="handleDelete({{ $data->id }})">{{ ts('delete') }}</span>
                    @endif
                @endif
                @if ($user_type == 2 && empty($delete_record) && $data->approved == 1)
                    <button type="button" class="btn btn-primary" onclick="handleDelete()">申请删除</button>
                @endif
            </div>

            <div class="modal fade" id="editCustomer">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title font-green">{{ ts('XGGKXX') }}</h5>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <div class="modal-body">
                            <form>
                                <input type="text" name="warranty_id" value="{{ $data->id }}" hidden>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('CUSTOMERNAME') }}</div>
                                    <div class="form-input">
                                        <input type="text" name="first_name" class="form-control" value="{{ $data->first_name }}">
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('phonenumber') }}</div>
                                    <div class="form-input">
                                        <div class=" input-group custom-control-inline">
                                            <div class="input-group-prepend">
                                                <select name="extension" class="form-control">
                                                    @foreach(\App\Models\PhoneExtension::get(['id', 'phone_ext']) as $v)
                                                        <option value="{{ $v->phone_ext }}" {{ $data->extension == $v->phone_ext ? 'selected' : '' }}>{{ $v->phone_ext }}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <input type="number" name="phone_number" value="{{ $data->phone_number }}" class="form-control">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label">{{ ts('email') }}</div>
                                    <div class="form-input">
                                        <input type="email" name="email_address" value="{{ $data->email_address }}" class="form-control">
                                    </div>
                                </div>

                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('region') }}</div>
                                    <div class="form-input">
                                        <select name="region_id" id="" class="form-control region_json" data-value="{{ $data->region_id }}">
                                            <option value="">{{ ts('select') }}</option>
                                            <option value="{{ $data->region_id }}" selected>{{ ts('region_master.'.$data->region_id) }}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('province') }}</div>
                                    <div class="form-input">
                                        <select name="province_id" id="" class="form-control province_json" data-value="{{ $data->province_id }}">
                                            <option value="">{{ ts('select') }}</option>
                                            <option value="{{ $data->province_id }}" selected>{{ ts('province_master.'.$data->province_id) }}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('city') }}</div>
                                    <div class="form-input">
                                        <select name="city" id="" class="form-control city_json" data-value="{{ $data->city }}">
                                            <option value="">{{ ts('select') }}</option>
                                            <option value="{{ $data->city }}" selected>{{ ts('cities.'.$data->city) }}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('addresss') }}</div>
                                    <div class="form-input">
                                        <input type="text" name="address" value="{{ $data->address }}" class="form-control">
                                    </div>
                                </div>
                                {{--<div class="form-item">
                                    <div class="form-label">{{ ts('zip') }}</div>
                                    <div class="form-input">
                                        <input type="text" name="zip" value="{{ $data->zip }}" class="form-control">
                                    </div>
                                </div>--}}

                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('licenseplate') }}</div>
                                    <div class="form-input">
                                        <input type="text" name="license_plate" value="{{ $data->license_plate }}" class="form-control">
                                    </div>
                                </div>
                            </form>
                            <div class="py-3 text-center">
                                <button type="button" class="btn btn-primary" onclick="handleEditCustomer()">{{ ts('confirm') }}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        @if(session('admin.user_type')!=2)
            <h3 class="section_sub_title">{{ ts('LSBGJL') }}</h3>
            <table class="table s3 text-center">
                <thead>
                <tr class="table-secondary">
                    <th>{{ ts('CZY') }}</th>
                    <th>{{ ts('CZe') }}</th>
                    <th>{{ ts('explain') }}</th>
                    <th>{{ ts('CZsj') }}</th>
                </tr>
                @foreach(getWSH($data->id) as $v)
                    <tr>
                        <td>{{ $v->abbr }}</td>
                        <td class="bg-light">{{ ts('urstatus'.$v->status) }}</td>
                        <td class="bg-light">
                            @if($v->status == 9 || $v->status == 10)
                                @foreach(json_decode($v->note, 1) ?: [] as $vn)
                                    <div><b>{{ @$vn['label'] }}</b>：{{ @$vn['old'] }} => {{ @$vn['new'] }}</div>
                                @endforeach
                            @else
                                {{ $v->note }}
                            @endif
                        </td>
                        <td>{{ fdate2($v->created_at) }}</td>
                    </tr>
                @endforeach
                </thead>
            </table>
        @endif


    </div>


@endsection

@section('script')
    <script>

        var WarrantyCustomerForm = @json($data);

        function handleEditCustomer() {
            var url = '/admin/ajax/editWarrantyCustomer';
            var form = formObj('#editCustomer form');
            var msg = '';
            if (!form.first_name.trim()) {
                msg = '客户名字不能为空';
            }
            if (!form.extension && !msg) {
                msg = '请选择电话号码地区号';
            }
            if (!form.phone_number && !msg) {
                msg = '电话号码不能为空';
            }
            if (!form.region_id && !msg) {
                msg = '请选择区域';
            }
            if (!form.province_id && !msg) {
                msg = '请选择省';
            }
            if (!form.city && !msg) {
                msg = '请选择城市';
            }
            if (!form.address && !msg) {
                msg = '请输入地址';
            }
            if (!form.license_plate && !msg) {
                // msg = '请输入车牌号';
            }
            if (form.extension == '+86') {
                if (form.license_plate && !msg && !myTestV1('license_plate', form.license_plate)) {
                    msg = '牌照号 格式不符合规范！';
                }
                if (form.phone_number && !msg && !myTestV1('phone_number', form.phone_number)) {
                    msg = '联系电话 格式不符合规范！';
                }
            }
            if (form.email_address && !msg) {
                var email = /^\w{1,}@\w{1,}\.\w{1,}$/;
                if (!email.test(form.email_address)) {
                    msg = '邮箱 格式不符合规范！';
                }
            }
            if (msg) {
                alert(msg)
                return false;
            }
            var fields = ['first_name','extension','phone_number','region_id','province_id','city','address','license_plate','email_address'];
            var state = true;
            for (var i=0; i<fields.length; i++) {
                var k = fields[i];
                if (typeof form[k] == 'string') {
                    form[k] = form[k].trim();
                }
                if (typeof WarrantyCustomerForm[k] == 'string') {
                    WarrantyCustomerForm[k] = WarrantyCustomerForm[k].trim();
                }
                if (form[k] != WarrantyCustomerForm[k]) {
                    state = false;
                }
            }
            if (state) {
                alert('信息没有变动！');
                return false;
            }

            // 修改后需要重新审核通过
            mySwal('确定修改车主信息？修改手机号码后会重新发送短信提醒！')
                .then(function (d) {
                    request.post(url, form)
                        .then(function (d) {
                            mySwal2()
                        })
                        .catch(function (d) {
                            var msg = d.msg || '修改失败，请刷新重试！';
                            alert(msg)
                            console.log(d)
                        })
                })
                .catch(function (d) {
                    console.log(d)
                })
        }

        function verifyTransfer(verify) {
            var url = '/{{ request()->path() }}/verify';
            mySwal()
                .then(function (d) {
                    request.post(url, {
                        verify: verify
                    })
                        .then(function (d) {
                            mySwal2()
                        })
                        .catch(function (d) {
                            sw3(d.msg, 'error');
                        })
                })
                .catch(function (d) {
                    console.log(d)
                })

            console.log(verify)
        }
        
        function handleDelete() {
            var url = '/admin/warranty_delete/';
            sweetAlert({
                title: '提交删除申请',
                content: "input",
                // icon: "warning",
                buttons: {
                    cancel: {
                        text: "<?php echo e(ts('CANCEL')); ?>",
                        value: false,
                        visible: true,
                        className: "",
                        closeModal: true,
                    },
                    confirm: {
                        text: "<?php echo e(ts('CONFIRM')); ?>",
                        value: true,
                        visible: true,
                        className: "",
                        closeModal: false
                    }
                },
            }).then(function (d) {
                if (!d) {
                    return;
                }
                request.post(url, {
                    warranty_id: '{{ $data->warranty_id }}',
                    reason: d,
                }).then( function(d) {
                    console.log('then', d)
                    sweetAlert({
                        title: "<?php echo e(ts('DELETESUCCESS')); ?>",
                        icon: "success",
                        button:'<?php echo e(ts('CONFIRM')); ?>'
                    }).then( function(d) {
                        window.location.reload()
                    })
                }).catch( function(d) {
                    if (d.code == 0) {
                        sweetAlert({
                            title: "操作成功",
                            icon: "success",
                            button:'<?php echo e(ts('CONFIRM')); ?>'
                        }).then( function(d) {
                            window.location.reload()
                        });
                    } else {
                        sweetAlert({
                            title: d.msg,
                            icon: "error",
                            button:'<?php echo e(ts('CONFIRM')); ?>'
                        })
                    }
                })
            }).catch(function (d) {
                console.log(d);
                if (!d) {
                    return;
                }
                sweetAlert({
                    title: "审核成功",
                    icon: "success",
                    button:'<?php echo e(ts('CONFIRM')); ?>'
                }).then( function(d) {
                    window.location.reload()
                })
            })
        }
    </script>
@endsection